/**
 * Created by FDG296 on 2017/8/28.
 */
var current_upload = '';                                                        //当前上传的是哪张图片

$(document).ready(function(){
    $.fn.dataTable.ext.errMode = 'none';
    $('#table_list').on( 'error.dt', function ( e, settings, techNote, message ) {
        $.jBox.error('系统异常，请联系管理员！', '错误');
        return false;
    } ).DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'/static/plugins/datatables/language.json?ver=001'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/goods-match/get-match-list',
            type:'post',
            data:function(d){
                d.search_set_name = $('#search_set_name').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "set_name", "bSortable":false },
            { "data": "img_main", "bSortable":false },
            { "data": "img_small", "bSortable":false },
            { "data": "sort", "bSortable":true },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="updateMatch('+ full.id +')">修改</a>&nbsp;&nbsp;' + '<a href="/goods-match/goods-list/?type=1&id='+ full.id + '">商品列表</a>';
                }
            },
            {
                "targets": [3],
                "render": function(data, type, full) {
                    return '<a href="javascript:" data-img="'+data+'" class="show_img_class">查看图片</a>';
                }
            },
            {
                "targets": [4],
                "render": function(data, type, full) {
                    return '<a href="javascript:" data-img="'+data+'" class="show_img_class">查看图片</a>';
                }
            },
            {
                "targets": [6],
                "data": "use_flg",
                "render": function(data, type, full) {
//                    var  title = '';
//                    if(data == 1){
//                        title = '禁用';
//                    }else{
//                        title = '启用';
//                    }
//                    return '<a href="javascript:" onclick="statusClick('+ full.id +','+ data + ')">' + title + '</a>';
                	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+data+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            }
        ],
        order: [[ 1, "desc" ]]                      //初始指定排序字段
    });
});

//查询处理
$('#select_button').click(function(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})

$('#reset_button').click(function(){
    $('#search_set_name').val('');
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})

//添加
$('#add_button').click(function(){
    resetForm();
    $('#myModal').modal('show');
})

//图片上传
//ICON图片上传按钮事件
$('.upload-img-btn-main').click(function(){
    current_upload = 'main';
    $('.upload_file_ot').trigger('click');
});
//LOGO图片上传按钮事件
$('.upload-img-btn-small').click(function(){
    current_upload = 'small';
    $('.upload_file_ot').trigger('click');
});

$('.upload_file_ot').change(function(){
    if(this.value == ''){

    }else{
        $('#upload_form_update').submit();
    }
});

/**
 * 图片上传动作
 **/
$('#upload_form_update').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        beforeSend:function(){
            if(current_upload == 'main'){
                $('.upload-img-btn-main').attr('disabled',true);
            }else{
                $('.upload-img-btn-small').attr('disabled',true);
            }
        },
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
                if(current_upload == 'main'){
                    $('.show-img-btn-main').attr('data-img','');
                    $('.show-img-btn-main').hide();
                    $('.upload-img-btn-main').attr('disabled',false);
                    $('.show-img-btn-main').attr('data-img',result.val);
                    $('#img_main').val(result.val);
                    $('.show-img-btn-main').show();
                }else{
                    $('.show-img-btn-small').attr('data-img','');
                    $('.show-img-btn-small').hide();
                    $('.upload-img-btn-small').attr('disabled',false);
                    $('.show-img-btn-small').attr('data-img',result.val);
                    $('#img_small').val(result.val);
                    $('.show-img-btn-small').show();
                }
            }
        }
    });
});

//查看icon图片
$('.show-img-btn-main').click(function(){
    var img_url = $(this).attr('data-img');
    showMoreImg(img_url);
});

//查看logo图片
$('.show-img-btn-small').click(function(){
    var img_url = $(this).attr('data-img');
    showMoreImg(img_url);
});

$('table').delegate('.show_img_class','click',function(){
    var img_url = $(this).attr('data-img');
    showMoreImg(img_url);
})

/**
 * 显示图片
 **/
function showMoreImg(objKey){
    if(objKey == '' || objKey == null || objKey == undefined){
        $.jBox.error('数据为空', '错误');
        return;
    }
    var json = {
        "title": "图片", //相册标题
        "id": 0, //相册id
        "start": 0, //初始显示的图片序号，默认0
        "data": [   //相册包含的图片，数组格式
            {
                "alt": "",//图片名
                "pid": 0, //图片id
                "src": $('#imageBathUrl').val()  + objKey, //原图地址
                "thumb": "" //缩略图地址
            }
        ]
    };
    layer.photos({
        photos: json
    });
}

$('.ajax_submit').click(function(){
    var set_name = $('#set_name').val();
    var img_main = $('#img_main').val();
    var img_small = $('#img_small').val();

    if(set_name == ''){
        $.jBox.error('请输入套餐名称', '错误');
        return false;
    }

    if(img_main == ''){
        $.jBox.error('请上传套餐主图', '错误');
        return false;
    }

    if(img_small == ''){
        $.jBox.error('请上传套餐小图', '错误');
        return false;
    }
    $.post($('#match_form').attr('action'),$('#match_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function(){
                self.location.reload();
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

//编辑
function updateMatch(id){
    $('#match_id').val(id);
    $.post('get-match-detail',{id:id},success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            var result = JSON.parse(data.val);
            $('#match_id').val(result.id);
            $('#set_name').val(result.set_name);
            $('#img_main').val(result.img_main);
            $('#img_small').val(result.img_small);
            $('#img_small_alt').val(result.img_small_alt);
            $('#img_main_alt').val(result.img_main_alt);
            $('#sort').val(result.sort);
            $('#link').val(result.link);
            $('#use_flg').val(result.use_flg);
            $('.show-img-btn-main').show();
            $('.show-img-btn-main').attr('data-img',result.img_main);
            $('.show-img-btn-small').show();
            $('.show-img-btn-small').attr('data-img',result.img_small);
            $('#myModal').modal('show');
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
}

//重置表单
function resetForm(){
    $('#match_id').val('');
    $('#set_name').val('');
    $('#img_main').val('');
    $('#img_small').val('');
    $('#img_small_alt').val('');
    $('#img_main_alt').val('');
    $('#sort').val('');
    $('#link').val('');
    $('#use_flg').val(1);
    $('.show-img-btn-main').hide();
    $('.show-img-btn-small').hide();

}

//表单赋值
function setForm(){

}

//启用禁用
function statusClick(id,use_flg){
    var title = '';
    var postUrl = '/goods-match/change-match-useflg';

    if(use_flg == 1){
        title = '确定禁用吗？';
        use_flg = 0;
    }else{
        title = '确定启用吗？';
        use_flg = 1;
    }

    $.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post(postUrl,{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();                             //关闭对话框
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () {
                        self.location.reload();
                    }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

$('#table_list').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	statusClick(id,use_flg);
});